﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServoTrack.WinForm
{
    public partial class dGWX : Form
    {
        public dGWX()
        {
            InitializeComponent();
        }

        private void dGWX_Load(object sender, EventArgs e)
        {
            gridViewMultitask.Rows.Clear();
            using (StreamReader rd = File.OpenText(Environment.CurrentDirectory + "\\text\\satelliteParameter.txt"))
            {
                for (int i = 0; ; i++)
                {
                    if (rd.EndOfStream) break; string line = rd.ReadLine();
                    string[] st = line.Split('%');
                    gridViewMultitask.Rows.Add();
                    gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[0].Value = gridViewMultitask.Rows.Count - 1;
                    gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[1].Value = st[0];
                    gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[2].Value = st[1];
                    gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[3].Value = st[2];
                    gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[4].Value = st[3];
                    gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[5].Value = st[4];
                }
            }
        }

        private void btnAddMultitask_Click(object sender, EventArgs e)
        {
            try
            {
                gridViewMultitask.Rows.Add();
                gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[0].Value = gridViewMultitask.Rows.Count - 1;
                gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[1].Value = txtFreq.Text;
                gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[2].Value = Convert.ToDouble(textBox1.Text);
                gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[3].Value = comboBox1.Text;
                gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[4].Value = Convert.ToDouble(numericUpDown1.Value);
                gridViewMultitask.Rows[gridViewMultitask.Rows.Count - 1].Cells[5].Value = Convert.ToDouble(numericUpDown2.Value);

            }
            catch (Exception s)
            {
                MessageBox.Show(s.Message, "消息提示");
            }
        }

        private void btnSelMultitask_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridViewMultitask.RowCount > 0)
                {
                    gridViewMultitask.Rows.RemoveAt(Convert.ToInt32(gridViewMultitask.SelectedRows[0].Cells[0].Value));
                    gridViewMultitask.Update();
                    for (int j = 0; j < gridViewMultitask.RowCount; j++)
                    {
                        gridViewMultitask.Rows[j].Cells[0].Value = j;
                    }
                    gridViewMultitask.SelectAll();
                }
            }
            catch (Exception err) { MessageBox.Show(err.Message, "消息提示"); }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridViewMultitask.RowCount > 0)
                {
                    gridViewMultitask.SelectedRows[0].Cells[0].Value = gridViewMultitask.Rows.Count - 1;
                    gridViewMultitask.SelectedRows[0].Cells[1].Value = txtFreq.Text;
                    gridViewMultitask.SelectedRows[0].Cells[2].Value = Convert.ToDouble(textBox1.Text);
                    gridViewMultitask.SelectedRows[0].Cells[3].Value = comboBox1.Text;
                    gridViewMultitask.SelectedRows[0].Cells[4].Value = Convert.ToDouble(numericUpDown1.Value);
                    gridViewMultitask.SelectedRows[0].Cells[5].Value = Convert.ToDouble(numericUpDown2.Value);
                }
                gridViewMultitask.SelectAll();
            }
            catch (Exception err) { MessageBox.Show(err.Message, "消息提示"); }
        }
        private void gridViewMultitask_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (gridViewMultitask.RowCount > 0)
                {
                    txtFreq.Text = gridViewMultitask.SelectedRows[0].Cells[1].Value.ToString();
                    textBox1.Text = gridViewMultitask.SelectedRows[0].Cells[2].Value.ToString(); ;
                    comboBox1.Text = gridViewMultitask.SelectedRows[0].Cells[3].Value.ToString(); ;
                    numericUpDown1.Value = (decimal)gridViewMultitask.SelectedRows[0].Cells[4].Value; ;
                    numericUpDown2.Value = (decimal)gridViewMultitask.SelectedRows[0].Cells[5].Value; ;
                }
            }
            catch (Exception err) { MessageBox.Show(err.Message, "消息提示"); }
        }

        private void dGWX_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "\\text\\satelliteParameter.txt", false, Encoding.UTF8))
                {
                    for (int i = 0; i < gridViewMultitask.Rows.Count; i++)
                    {
                        string st = gridViewMultitask.Rows[i].Cells[1].Value + "%" +
                            gridViewMultitask.Rows[i].Cells[2].Value + "%" +
                            gridViewMultitask.Rows[i].Cells[3].Value + "%" +
                            gridViewMultitask.Rows[i].Cells[4].Value + "%" +
                            gridViewMultitask.Rows[i].Cells[5].Value;
                        sw.WriteLine(st);
                    }
                }
            }
            catch (Exception err) { MessageBox.Show(err.Message, "消息提示"); }
        }
    }
}
